package com.eling.elcms.cater.service.impl;

import com.eling.elcms.cater.dao.IDishMenuDao;
import com.eling.elcms.cater.model.Dish;
import com.eling.elcms.cater.model.DishMenu;
import com.eling.elcms.cater.model.DishPrice;
import com.eling.elcms.cater.service.IDishMenuService;
import com.eling.elcms.cater.service.IDishPriceService;
import com.eling.elcms.cater.service.IDishService;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DishMenuServiceImpl extends GenericManagerImpl<DishMenu, Long> implements IDishMenuService{
	
	IDishMenuDao dishMenuDao;
	
	@Autowired
	IDishService dishService;
	
	@Autowired
	IDishPriceService dishPriceService;
	
	@Autowired
	public void setDao(IDishMenuDao dao) {
		this.dao = dao;
		this.dishMenuDao =dao;
	}

	@Override
	public List<DishMenu> queryOneWeek(DishMenu dishMenu) {
		List<DishMenu> returnList = new ArrayList<>();
		Date menuDate = dishMenu.getMenuDate();
		/*for(int i = 0;i<7;i++){//需要周一的日期，然后日期加天数
			if(i!=0){
				menuDate = addOneDay(menuDate, 1);
			}
			dishMenu.setMenuDate(menuDate);
			PropertyUtils.setProperty(dishMenu, "menuDateEnd", menuDate);
			List<DishMenu> list = query(dishMenu);
			if(list!=null && !list.isEmpty()){
				returnList.add(list.get(0));
			}else{
				returnList.add(dishMenu);
			}
		}*/
		Date menuDateEnd = addOneDay(menuDate, 6);
		PropertyUtils.setProperty(dishMenu, "menuDate", menuDate);
		PropertyUtils.setProperty(dishMenu, "menuDateEnd", menuDateEnd);
		List<DishMenu> list = query(dishMenu);
		flag:
		for(int i = 0; i < 7; i ++){
			if(i!=0){
				menuDate = addOneDay(menuDate, 1);
				dishMenu.setMenuDate(menuDate);
			}
			for(int j = 0; j < list.size();j++){
				if(list.get(j).getMenuDate().getTime()==menuDate.getTime()){
					returnList.add(list.get(j));
					continue flag;
				}
			}
			returnList.add(dishMenu);
		}
		return returnList;
	}

	@Override
	public DishMenu saveOneDay(String string,DishMenu dishMenu) {
		String[] menu = string.split(",");
		List<Long> pkList = new ArrayList<>();
		for (int i = 0; i < menu.length; i++) {
			pkList.add(Long.parseLong(menu[i]));
		}
		//PropertyUtils.setProperty(dishMenu, "dishes.pkDishIn", pkList);//这个行不通
		Set<Dish> dishs = new HashSet<>();
		for (Long l : pkList) {
			Dish dish = new Dish();
			dish.setPkDish(l);
			List<Dish> query = dishService.query(dish);
			if(query!=null && !query.isEmpty()){
				dishs.add(query.get(0));
			}
		}
		List<DishMenu> query = query(dishMenu);
		if(query!=null && !query.isEmpty()){
			dishMenu=query.get(0);
			dishMenu.setDishes(dishs);
			return save(dishMenu);
		}else{
			dishMenu.setDishes(dishs);
			return save(dishMenu);
		}
	}

	@Override
	public void removeOneDay(DishMenu dishMenu) {
		PropertyUtils.setProperty(dishMenu, "menuDateEnd", dishMenu.getMenuDate());
		List<DishMenu> list = query(dishMenu);
		Set<Dish> set = new HashSet<Dish>();
		if(list!=null && !list.isEmpty()){
			dishMenu = list.get(0);
			dishMenu.setDishes(set);
			save(dishMenu);
		}
	}

	@Override
	public void CopyToNextWeek(DishMenu dishMenu) {
		PropertyUtils.setProperty(dishMenu, "menuDateEnd", dishMenu.getMenuDate());
		List<DishMenu> list = query(dishMenu);
		List<DishMenu> weekList = new ArrayList<DishMenu>();
		if(list!=null && !list.isEmpty()){
			weekList.add(list.get(0));
		}else{
			weekList.add(new DishMenu());
		}
		Date menuDate = dishMenu.getMenuDate();
		for(int i = 0; i < 6; i++){//将本周的放到集合中
			menuDate = addOneDay(menuDate,1);
		    DishMenu dMenu = new DishMenu();
		    dMenu.setMenuDate(menuDate);
		    PropertyUtils.setProperty(dMenu, "menuDateEnd", dMenu.getMenuDate());
		    List<DishMenu> dList = query(dMenu);
		    if(dList!=null && !dList.isEmpty()){
		    	weekList.add(dList.get(0));
		    }else{
		    	weekList.add(dMenu);
		    }
		}
		for(int i = 0; i < 7;i++){//集合中天数加7，再保存
			DishMenu dMenu = new DishMenu();
			Date date = weekList.get(i).getMenuDate();
			if(date!=null){
				dMenu.setMenuDate(addOneDay(date,7));
			}
			PropertyUtils.setProperty(dMenu, "menuDateEnd", dMenu.getMenuDate());
			if(query(dMenu)==null || query(dMenu).isEmpty()){
				dMenu.setPkDishMenu(null);
				dMenu.setVersion(0);
				if(weekList.get(i).getServicePoint()!=null){
					dMenu.setServicePoint(weekList.get(i).getServicePoint());
				}if(weekList.get(i).getDishes()!=null){
					Set<Dish> dishes = weekList.get(i).getDishes();
					Set<Dish> set = new HashSet<>();
					for (Dish dish : dishes) {
						set.add(dish);
					}
					dMenu.setDishes(set);
				}
				save(dMenu);
			}else {
				dMenu.setPkDishMenu(query(dMenu).get(0).getPkDishMenu());
				dMenu.setVersion(query(dMenu).get(0).getVersion());
				if(weekList.get(i).getServicePoint()!=null){
					dMenu.setServicePoint(weekList.get(i).getServicePoint());
				}if(weekList.get(i).getDishes()!=null){
					Set<Dish> dishes = weekList.get(i).getDishes();
					Set<Dish> set = new HashSet<>();
					for (Dish dish : dishes) {
						set.add(dish);
					}
					dMenu.setDishes(set);
				}
				save(dMenu);
			}
		}
		
	}
	
	public Date addOneDay(Date day,int num){
		Calendar  calendar = new GregorianCalendar(); 
	    calendar.setTime(day); 
	    calendar.add(Calendar.DATE,num);//把日期往后增加一天.整数往后推,负数往前移动 
	    day = calendar.getTime();   //这个时间就是日期往后推一天的结果 
	    return day;
	}

	@Override
	public List<DishPrice> querytodaydish(DishMenu cond) {
		//找今天的菜单
		List<DishMenu> dishMenuList = query(cond);
		if(CollectionUtils.isEmpty(dishMenuList)) {
			return Collections.emptyList();
		}
		//今天菜单只有一个值，拿到今天的菜品。
		DishMenu dishMenu = dishMenuList.get(0);
		List<Long> dishPks = dishMenu.getDishes().stream().map(a->a.getPkDish()).collect(Collectors.toList());
		//查找对应的菜价，拼装返回值。
		DishPrice dishPrice = new DishPrice();
		PropertyUtils.setProperty(dishPrice, "dish.pkDishIn", dishPks);
		
		return dishPriceService.query(dishPrice);
	}
}
